import { createContainer } from "react-tracked"
import { ImmerReducer, useImmerReducer } from "use-immer"

interface InitialValue {
  fields: any[],
  rules: any
}

const initialValue: InitialValue = {
  fields: [],
  rules: {}
}

type TAction = {
  type: 'addField' | 'addRules',
  field?: any
  rules?: any
}

const reducer: ImmerReducer<typeof initialValue, TAction> = (state, action) => {
  switch (action.type) {
    case "addField":
      state.fields.push(action.field)
      break
    case "addRules":
      state.rules = action.rules
      break
    default:
      break
  }
}

const useValue = () => {
  return useImmerReducer(reducer, initialValue)
}

export const formContextProvider = createContainer(useValue)
